﻿using System.Runtime.InteropServices;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using WindowsPhoneToolbox.Controls;
using Microsoft.VisualStudio.Shell;
using WindowsPhoneToolbox.Util;

namespace WindowsPhoneToolbox
{
    /// <summary>
    /// This class implements the tool window exposed by this package and hosts a user control.
    ///
    /// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane, 
    /// usually implemented by the package implementer.
    ///
    /// This class derives from the ToolWindowPane class provided from the MPF in order to use its 
    /// implementation of the IVsUIElementPane interface.
    /// </summary>
    [Guid("5438a6b4-4fab-4a32-8e22-7cf82f65e556")]
    public class WindowPhoneToolWindow : ToolWindowPane
    {
        private readonly WindowsPhoneControl _windowsPhone;
        private bool _enableEffects;
        private readonly DTE2 _dte;

        /// <summary>
        /// Standard constructor for the tool window.
        /// </summary>
        public WindowPhoneToolWindow() :
            base(null)
        {
            // Set the window title reading it from the resources.
            Caption = Resources.ToolWindowTitle;
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            BitmapResourceID = 301;
            BitmapIndex = 1;

            _dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE)) as DTE2;
            if (_dte != null)
                _dte.Events.WindowEvents.WindowActivated += WindowEventsWindowActivated;

            // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
            // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
            // the object returned by the Content property.
            _windowsPhone = new WindowsPhoneControl();

            base.Content = _windowsPhone;
        }

        private void WindowEventsWindowActivated(EnvDTE.Window gotFocus, EnvDTE.Window lostFocus)
        {
            if (gotFocus.Caption == Caption)
            {
                _windowsPhone.Focus();
            }
        }

        // Enable/disable animation effects on the progress bar
        public bool EffectsEnabled
        {
            get { return _enableEffects; }
            set
            {
                _enableEffects = value;
                _windowsPhone.AnimateColor = _enableEffects;
            }
        }

        // Set the progress bar value
        public double Progress
        {
            get { return _windowsPhone.Value; }
            set { _windowsPhone.Value = value; }
        }

        // Set the progress bar text
        public string BarText
        {
            get { return _windowsPhone.Text; }
            set { _windowsPhone.Text = value; }
        }

        public string StatusText
        {
            get { return _windowsPhone.CelPhoneMsg.Text; }
            set { _windowsPhone.CelPhoneMsg.Text = value; }
        }

        public bool HasSolution()
        {
            return _windowsPhone.PanelEditManifest.HasSolution;
        }


        public void ShowBuilding(bool show)
        {
            _windowsPhone.ShowPanelBuild(show);
        }

        public void ShowPhoneStatus(bool show)
        {
            _windowsPhone.ShowPanelPhone(show);
        }

        public void ShowPhoneConnect(bool show)
        {
            _windowsPhone.ShowPanelConnect(show);
        }

        public void Connect(string fileManifest, string targetDevice)
        {
            _windowsPhone.TargetConnect(fileManifest, targetDevice);
        }

        public void ShowWelComeTimer(int seconds)
        {
            _windowsPhone.ShowPanelWelcomeTimer(seconds);
        }


    }
}